package com.smartEdu.controller.app;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.smartEdu.commons.LogCvt;
import com.smartEdu.commons.Session;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.exception.RequestException;

public abstract class AppBaseController {
	public void SetResponse(HttpServletResponse response,Session sess){
		try {
			String s=GetRespContent(sess);
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(s);
			LogCvt.info("响应数据:"+s);
		} catch (IOException e) {
			LogCvt.error(e.getMessage());
		}
	}
	
	public Session GetSession(){
		return (Session)((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest().getAttribute("sess");
	}
	
	public String GetParameter(HttpServletRequest request) {
		String reqContent = null;
		try {
			request.setCharacterEncoding("utf-8");
			reqContent = request.getParameter(Tag_Tips.REQUEST);
			if (reqContent == null) {
				throw new RequestException("请求参数为空");
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return reqContent;
	}
	
	public abstract String GetRespContent(Session sess);
}